package com.hippo.scr.article.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.xml.stream.XMLStreamException;

import nl.siegmann.epublib.domain.Author;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.epub.EpubWriter;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.hippo.scr.article.document.Article;
import com.hippo.scr.article.mongorepository.ArticleRepository;
import com.hippo.scr.article.value.ArticlePageValue;
import com.hippo.tech.framework.controller.HomeController;
import com.hippo.tech.framework.document.AppUser;

@Service
public class ArticleService {
	
	private static Logger logger = Logger.getLogger(HomeController.class);
	
	@Autowired
	Environment environment;
	
	@Autowired 
	private ArticleRepository articleRepository;
	
	public void save(Article article) {
		articleRepository.save(article);
	}
	
	public List<Article> findAll() {
		return articleRepository.findAll();
	}
	
	public void saveEpub(ArticlePageValue articlePage) throws IOException, XMLStreamException {
		String epubRoot = environment.getProperty("epubFilesRootFolder");
		logger.info("epub root folder: " + epubRoot); 
		
		AppUser currentUser = articlePage.getAppUser();
		if(articlePage.getId() != null && !(articlePage.getId().equals(""))) {
			logger.info("Article already exists. Getting reference of epub");
			
		} else {
			
			logger.info("New Article. Creating new epub");
			Book book = new Book();
			// Setting title of the book. 
			book.getMetadata().addTitle(articlePage.getTitle());
			
			// Setting the author of the book
			book.getMetadata().addAuthor(new Author(currentUser.getFirstName(),currentUser.getLastName()) ); // TODO: change to the first name and last name of the current user. 
			
			// Creating a new page
			book.addSection("Intro", new Resource(articlePage.getArticleContent().getBytes(), "Introduction.html"));
			
			// Writing the page into the epub file. 
			
			File epubFile = new File(epubRoot + "/" + currentUser.getId() + "/" + articlePage.getTitle().replace(' ', '_') + ".epub");
			if(!epubFile.exists()) {
				epubFile.createNewFile();
			} 


			EpubWriter epubWriter = new EpubWriter();
			epubWriter.write(book, new FileOutputStream(epubFile));
		}

		/*			InputStream is = new ByteArrayInputStream(articlePage.getArticleContent().getBytes());
		// read it with BufferedReader
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		
		String fileName = "C:/ABlog/articleextract/deiveehan/Introduction.html";
		FileOutputStream fos = new FileOutputStream(fileName);
		fos.write(articlePage.getArticleContent().getBytes());
		fos.close();*/
		
		// Adding the first page of the book. 

	}
}
